home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6889 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  83 lines

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Separate initization from Instantiation
  4. Message-ID: <1996Feb20.110543.1779@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 20 Feb 96 11:05:43 WET
  7. References: <4g8l8s$e5q@reader2.ix.netcom.com>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4g8l8s$e5q@reader2.ix.netcom.com>  
  12. pwallin@ix.netcom.com(Patrick Wallin ) writes:
  13.  
  14. > I am researching on initization and instantiation in C++.
  15.  
  16. [snip]
  17.  
  18. > So I am trying to figure out how to allocate first and then later we
  19. > initialize something.  I would appreciate if you could give me a short
  20. > example of it.
  21.  
  22. To do this, you should understand the difference between a direct call to  
  23. operator new()
  24.  
  25.     void *vp = operator new(sizeof *T);
  26.  
  27. which simply allocates memory and returns a void * to it, and a  
  28. `new-expression'
  29.  
  30.     T *tp = new T;
  31.  
  32. which first allocates memory by a call to operator new() and then  
  33. constructs an object in that memory, returning the address of the
  34. constructed object.
  35.  
  36. Furthermore, you should know that it's possible to overload operator  
  37. new(). Its first argument must be of type size_t and tells us how much  
  38. memory to allocate, but we are free to declare versions of operator new()  
  39. taking additional arguments. When called from a `new-expression' the first  
  40. argument to operator new() is passed to us by the compiler, and the  
  41. following ones are in between parentheses after the `new' keyword.  If we  
  42. write:
  43.  
  44.     T *tp = new (x) T;
  45.  
  46. The compiler will call `operator new(sizeof(T), x)' before constructing an  
  47. object of type T in the memory allocated.  
  48.  
  49. Finally, here's how this all adds up:
  50.  
  51.     // This version of operator new() simply returns the address we
  52.     // pass to it in its second argument. It is often called
  53.     // `placement new' and usually defined in <new.h>
  54.     inline void *operator new(size_t, void *p) 
  55.         { return p; } 
  56.  
  57.     class MyClass {
  58.         ...
  59.     public :
  60.         MyClass(int arg1, int arg2);
  61.         ...
  62.         ~MyClass();
  63.     };
  64.  
  65.     void f()
  66.     {
  67.         // get the memory we need by a direct call to 
  68.         // the default operator new()
  69.         void *buf = operator new(sizeof MyClass);
  70.  
  71.         // .. do other things...
  72.  
  73.         // call a constructor by writing a `new-expression'
  74.         // calling `placement new'
  75.         MyClass *mcp = new (buf) MyClass(1, 2);
  76.  
  77.         // ...use *mcp...
  78.     }
  79.  
  80. Hope this helps,
  81.  
  82. - Wil
  83.